/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.is.cef.command.listener;

import cef.command.Command;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import coalescent.is.cef.command.RunSamplers_Cmd;
import coalescent.is.cef.event.RunSamplers_Evt;
import commons.util.FormatUtil;

import java.math.BigDecimal;

/**
 * @author Susanta Tewari
 * @version version
 * @history Created on Aug 6, 2012.
 * @since since
 */
public class RunSamplers_PW extends CommandPrintWriter {

    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdEvent)
            throws CommandOutputListenerException {

        final RunSamplers_Cmd cmd             = (RunSamplers_Cmd) cmdEvent.getSource();
        final RunSamplers_Evt.GUI guiSettings = cmd.getEvent().getGUISettings();


        // exact, if available
        if (guiSettings.isExactValueKnown()) {

            final BigDecimal exactProb = guiSettings.getExactValue();

            getPrintWriter().println("Exact value: " + FormatUtil.format(exactProb));
        }


        // print mean
        getPrintWriter().println("\nMean: ");

        for (final String samplerName : cmd.getSamplerNames()) {

            final BigDecimal isMean = cmd.getISMean(samplerName);

            getPrintWriter().println(samplerName + ": " + FormatUtil.format(isMean));
        }


        // print SE
        getPrintWriter().println("\nStandard Error: ");

        for (final String samplerName : cmd.getSamplerNames()) {

            final BigDecimal isSE = cmd.getIS_SE(samplerName);

            getPrintWriter().println(samplerName + ": " + FormatUtil.format(isSE));
        }


        // print ESS
        getPrintWriter().println("\nEffective Sample Size: ");

        for (final String samplerName : cmd.getSamplerNames()) {

            final BigDecimal isESS = cmd.getIS_ESS(samplerName);

            getPrintWriter().println(samplerName + ": " + FormatUtil.format(isESS));
        }


        // time
        String msg = "\nTime (in seconds)";

        if (RunSamplers_Cmd.isThreadCpuTimeEnabled()) msg += " [using Thread-CPU-Time]: ";
        else msg += " [using Elapsed-Time]: ";

        getPrintWriter().println(msg + "\n");

        for (final String samplerName : cmd.getSamplerNames()) {

            final long elapsedTime = cmd.getElapsedTime(samplerName);

            getPrintWriter().println(samplerName + ": " + FormatUtil.format(elapsedTime));
        }
    }

    @Override
    public CommandOutputListener newInstance() {
        return new RunSamplers_PW();
    }

    @Override
    public Class<? extends Command> getTargetCommandClass() {
        return RunSamplers_Cmd.class;
    }
}
